<?php

namespace User\Controller;

use Think\Controller;
use User\Model\FactoryModel;
use User\Model\JTModel;

//header('Access-Control-Allow-Origin: *');

class JTController extends Controller
{
    public function __construct()
    {
        parent::__construct();
        $beyond_arr = ["login", "ajaxLogin", "verifyCode"];//---需要被排除登陆检查的方法--
        if (in_array(ACTION_NAME, $beyond_arr)) {

        } else {
            $is_login = session("jt_login");
            if (!$is_login) {//---未登录跳转---
                if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest') {
                    $this->ajaxReturn(["code" => -99]);
                } else {
                    $this->redirect("/User/JT/login");
                }
            }
        }
    }

    public function login()
    {
        C('LAYOUT_ON', false);
        $this->display();
    }

    public function ajaxLogin()
    {
        $email = I("post.email", "", "");
        $passwd = I("post.password", "", "");
        $code = I("post.code", "", "");
        $session_code = session('verifyCode');
        if (($email === "liqian@8531.com") && ($passwd === md5("liqian1996")) && ($code==$session_code)) {//--用户名密码验证---
            session("jt_login", 1);
            $this->ajaxReturn(["code" => 1]);
        } else {
            $this->ajaxReturn(["code" => -1]);
        }
    }
    //----获取验证码---
    public function verifyCode()
    {
        $im = imagecreate($x = 90, $y = 35);
        imagecolorallocate($im, rand(50, 200), rand(0, 155), rand(0, 155)); //第一次对 imagecolorallocate() 的调用会给基于调色板的图像填充背景色
        $fontColor = imagecolorallocate($im, 255, 255, 255); //字体颜色
        $fontstyle = '/Public/fonts/verdanaz.ttf';
        $authcode = '';

        for ($i = 0; $i < 4; $i++) {
//            $randAsciiNumArray = array(rand(48, 57), rand(65, 90));
            $randAsciiNum = rand(48, 57);
            $randStr = chr($randAsciiNum);
            $authcode .= $randStr;
        }
        imagefttext($im, 19, 0, 5, 25, $fontColor, $fontstyle, $authcode);
        //--干扰线--
        for ($i = 0; $i < 8; $i++) {
            $lineColor = imagecolorallocate($im, rand(0, 255), rand(0, 255), rand(0, 255));
            imageline($im, rand(0, $x), 0, rand(0, $x), $y, $lineColor);
        }
        //--干扰点--
        for ($i = 0; $i < 250; $i++) {
            imagesetpixel($im, rand(0, $x), rand(0, $y), $fontColor);
        }
        ob_clean();
        header('Content-type: image/png');
        imagepng($im);
        imagedestroy($im);
        session('verifyCode', $authcode);
    }

    //---获取新闻列表页内容--
    private function pri_news_index()
    {
        $page = I("get.page", 1, "");
        $cond['status'] = array("NEQ", 9);
        $title = I("get.title", "", "");
        if (!empty($title)) {
            $cond['title'] = array("LIKE", "%$title%");
        }
        $author = I("get.author", "", "");
        if (!empty($author)) {
            $cond['author'] = $author;
        }
        $start_time = I("get.start_time", "", "");
        $end_time = I("get.end_time", "", "");
        if (!empty($start_time) && !empty($end_time)) {
            $start_time = $start_time . " 00:00:00";
            $end_time = $end_time . " 23:59:59";
            $cond['add_time'] = array("between", [$start_time, $end_time]);
        } elseif (!empty($start_time)) {
            $start_time = $start_time . " 00:00:00";
            $cond['add_time'] = array("EGT", $start_time);
        } elseif (!empty($end_time)) {
            $end_time = $end_time . " 23:59:59";
            $cond['add_time'] = array("ELT", $end_time);
        }
        $pttl_id = I("get.pttl_id", "", "");
        if (!empty($pttl_id)) {
            $cond['pttl_id'] = $pttl_id;
        }
        $ttl_id = I("get.ttl_id", "", "");
        if (!empty($ttl_id)) {
            $cond['ttl_id'] = $ttl_id;
        }
        $status = I("get.status", -1, "");
        if ($status != -1) {
            $cond['status'] = $status;
        }
        $clock = I("get.clock", "", "");
        if (!empty($clock)) {
            $cond['clock'] = $clock;
        }
        $start_find = $page ? (($page - 1) * 10) : 0;
        $count = M("jt_news")->where($cond)->count();
        $news = M("jt_news")->where($cond)->limit($start_find, 10)
            ->field("id,title,pttl_id,ttl_id,author,img,desc,clock,status,add_time,type")->order("add_time desc")->select();
        foreach ($news as &$new) {
            $new['pttl_name'] = $new['ttl_name'] = "";
            $cond_pttl['id'] = $new['pttl_id'];
            $pttl = M("jt_title")->where($cond_pttl)->find();
            if (!empty($pttl)) {
                $new['pttl_name'] = $pttl['name'];
            } else {
                $new['pttl_name'] = "";
            }

            $cond_ttl['id'] = $new['ttl_id'];
            if ($cond_ttl['id'] != 0) {
                $ttl = M("jt_title")->where($cond_ttl)->find();
                if (!empty($ttl)) {
                    $new['ttl_name'] = $ttl['name'];
                }
            } else {
                $new['ttl_name'] = "";
            }
            $new['url'] = C("SITE_URL") . "/Wap/JT/clock?news_id=" . $new['id'] . "&token=" . md5($new['id']);
        }
        $cond_title['status'] = 1;
        $cond_title['level'] = 0;
        $p_title_arr = M("jt_title")->where($cond_title)->order("priority asc")->field("id, name")->select();
        $status_arr = JTModel::get_status();
        //--返回新闻个数、新闻列表、父级菜单、状态列表--
        $res = ['count' => $count, 'news' => $news, 'p_title_arr' => $p_title_arr, 'status_arr' => $status_arr];
        return $res;
    }

    public function index()
    {
        C('LAYOUT_ON', false);
//        $res = $this->pri_news_index();
//        $this->assign("res", json_encode($res));
        $this->display();
    }

    public function ajaxGetNewsList()
    {
        $res = $this->pri_news_index();
        $this->ajaxReturn(["res" => $res]);
    }

    //---获取栏目名称列表---
    public function ajaxGetTitleListBylevel()
    {
        $level = I("get.level", 0, "");
        $p_id = I("get.p_id", "", "");
        $res = ["ttl_arr" => []];
        if (!empty($p_id) && ($level == 1)) {
            $cond['level'] = 1;
            $cond['status'] = 1;
            $cond['parent_id'] = $p_id;
        } else {
            $cond['level'] = 0;
            $cond['status'] = 1;
        }
        $res["ttl_arr"] = M("jt_title")->where($cond)->order("priority asc")->field("id, name")->select();
        $this->ajaxReturn($res);
    }

    //---获取新闻内容详情--
    private function pri_news()
    {
        $id = I("get.id", "", "");
        $news = [];
        if (!empty($id)) {
            $cond['id'] = $id;
            $news = M("jt_news")->where($cond)->find();
            if (!empty($news)) {
                $news['pttl_name'] = $news['ttl_name'] = "";
                $cond_pttl['id'] = $news['pttl_id'];
                $pttl = M("jt_title")->where($cond_pttl)->find();
                if (!empty($pttl)) {
                    $news['pttl_name'] = $pttl['name'];
                } else {
                    $new['pttl_name'] = "";
                }

                $cond_ttl['id'] = $news['ttl_id'];
                if ($cond_ttl['id'] != 0) {
                    $ttl = M("jt_title")->where($cond_ttl)->find();
                    if (!empty($ttl)) {
                        $news['ttl_name'] = $ttl['name'];
                    }
                } else {
                    $new['ttl_name'] = "";
                }
            }
        }
        return $news;
    }

    public function newsDetail()
    {
        $news = $this->pri_news();
        $this->assign("res", json_encode($news));
        $this->display();
    }

    public function ajaxGetNewsDetail()
    {
        $news = $this->pri_news();
        $this->ajaxReturn(["res" => $news]);
    }

    public function ajaxNewsPreview()
    {
        $news_id = I("get.news_id", "", "");
        Vendor("phpqrcode.phpqrcode");
        $QRcode = new \QRcode();
        $url = C("SITE_URL") . "/Wap/JT/clock?news_id=" . $news_id . "&visitor=1";
        // 纠错级别：L、M、Q、H
        $level = 'L';
        // 点的大小：1到10,用于手机端4就可以了
        $size = 4;
        $QRcode::png($url, false, $level, $size);
    }

    public function ajaxNewsUpdate()
    {
        $data['title'] = I("post.title", "", "");
        $data['author'] = I("post.author", "", "");
        $data['url'] = I("post.url", "", "");
        $data['pttl_id'] = I("post.pttl_id", "", "");
        $data['ttl_id'] = I("post.ttl_id", "", "");
        $data['type'] = I("post.type", 0, "");
        $data['content'] = I("post.content", "", "");
        $data['desc'] = I("post.desc", "", "");
        $data['clock'] = I("post.clock", 0, "");
        $img = I("post.img", "", "");
        if (!empty($img)) {
            $ajaxImg = FactoryModel::ajaxImgUpload($img, "jt");
            if ($ajaxImg['code'] != 1) {
                return $this->ajaxReturn(["code" => -1, "desc" => "图片上传失败"]);
            }
            $data['img'] = $ajaxImg['msg'];
        }
        $id = I("post.id", "", "");
        if (empty($id)) {
            $res1 = $res2 = 1;
            M()->startTrans();
            $res1 = M("jt_news")->add($data);
            if ($data['clock']) {
                $cond_dept['status'] = 1;
                $cond_dept['name'] = array("NEQ", "集团总部");
                $dept_list = M("jt_dept")->where($cond_dept)->select();
                $data_list = array();
                foreach ($dept_list as $dept) {
                    $dept_data['dept_id'] = $dept['id'];
                    $dept_data['number'] = $dept['number'];
                    $dept_data['new_id'] = $res1;
                    $dept_data['type'] = $data['type'];
                    $data_list[] = $dept_data;
                }
                $res2 = M("jt_dept_data")->addAll($data_list);
            }
            M()->commit();

        } else {//---文章信息保存---
            unset($data['clock']);
            unset($data['type']);
            M("jt_news")->where(['id' => $id])->save($data);
        }
        $this->ajaxReturn(["code" => 1, "desc" => $id]);
    }

    public function ajaxNewsStatus()
    {
        $id = I("post.id", "", "");
        $data['status'] = I("post.status", "", "");//---0:待发布，1发布，2.置顶，9删除----
        $data['update_time'] = date("Y-m-d H:i:s");
        if (!empty($id)) {
            $id_arr = explode(",", $id);
            $cond['id'] = array("IN", $id_arr);
            M("jt_news")->where($cond)->save($data);

            if ($data['status'] == 9) {
                if (!empty($id_arr)) {
                    $cond_dept_data['new_id'] = array("IN", $id_arr);
                    M("jt_dept_data")->where($cond_dept_data)->save($data);
                }
            }
        }
        $this->ajaxReturn(["code" => 1, "desc" => "成功"]);
    }

    //---获取栏目列表页内容--
    private function pri_title_index()
    {
        $page = I("get.page", 1, "");
        $cond['status'] = array("NEQ", 9);
        $cond['level'] = 0;
        $start_find = $page ? (($page - 1) * 10) : 0;
        $count = M("jt_title")->where($cond)->count();
        $title_arr = M("jt_title")->where($cond)->order("priority asc")->select();
//        $title_arr = M("jt_title")->where($cond)->limit($start_find, 10)->order("priority asc")->select();
        foreach ($title_arr as &$title) {
            $title['url'] = C("SITE_URL") . "/Wap/JT/index?id=" . $title['id'];
            $cond_sun_title['parent_id'] = $title['id'];
            $cond_sun_title['level'] = 1;
            $cond_sun_title['status'] = 1;
            $sun_title = M("jt_title")->where($cond_sun_title)->order("priority asc")->select();
            $title['sun'] = $sun_title;
        }
        $res = ["titleList" => $title_arr, "count" => $count];
        return $res;
    }

    public function title()
    {
//        $res = $this->pri_title_index();
//        $this->assign("res", json_encode($res));
        $this->display();
    }

    public function ajaxGetTitleList()
    {
        $res = $this->pri_title_index();
        $this->ajaxReturn(["res" => $res]);
    }

    public function ajaxTitleUpdate()
    {
        $id = I("post.id", "", "");
        if (!empty($id)) {//---修改內容--
            $priority = I("post.priority", "", "");
            $name = I("post.name", "", "");
            $status = I("post.status", "", "");
            if (($priority !== "") || !empty($name) || !empty($status)) {
                if (($priority !== "")) {
                    $data['priority'] = $priority;
                    M("jt_title")->where(['id' => $id])->save($data);
                }
                if (!empty($name)) {
                    $data['name'] = $name;
                    M("jt_title")->where(['id' => $id])->save($data);
                }
                if (!empty($status)) {
                    $id_arr = explode(",", $id);
                    $cond['id'] = array("IN", $id_arr);
                    $data['status'] = $status;
                    M("jt_title")->where($cond)->save($data);
                }
            }
        } else {
            $data['name'] = I("post.name", "", "");
            $cond['level'] = $data['level'] = I("post.level", 0, "");
            $cond['parent_id'] = $data['parent_id'] = I("post.parent_id", 0, "");
            $data['priority'] = M("jt_title")->where($cond)->max("priority");
            $data['priority'] = $data['priority'] + 1;
            M("jt_title")->add($data);
        }
        $this->ajaxReturn(["code" => 1, "desc" => "成功"]);
    }


    //----获取栏目banner图列表----
    private function pri_getBannerList()
    {
        $id = I("get.title_id", "", "");
        $cond['title_id'] = $id;
        $cond['status'] = 1;
        $banners = M("jt_title_banner")->where($cond)->order("priority desc,add_time desc")->select();
        return $banners;
    }

    public function banner()
    {
        $this->display();
    }

    public function ajaxGetBanners()
    {
        $res = $this->pri_getBannerList();
        $this->ajaxReturn(["res" => $res]);
    }

    private function pri_banner()
    {
        $id = I("get.id", "", "");
        $banner = [];
        if (!empty($id)) {
            $cond['id'] = $id;
            $cond['status'] = 1;
            $banner = M("jt_title_banner")->where($cond)->find();
        }
        return $banner;
    }

    public function bannerDetail()
    {
        $banner = $this->pri_banner();
        $this->assign("res", json_encode($banner));
        $this->display();
    }

    public function ajaxGetBannerDetail()
    {
        $banner = $this->pri_banner();
        $this->ajaxReturn(["res" => $banner]);
    }

    public function ajaxBannerUpdate()
    {
        $data['url'] = I("post.url", "", "");
        $data['title_id'] = I("post.title_id", "", "");
        $data['name'] = I("post.name", "", "");
        $img = I("post.img", "", "");
        if (!empty($img)) {
            $ajaxImg = FactoryModel::ajaxImgUpload($img, "jt");
            if ($ajaxImg['code'] != 1) {
                return $this->ajaxReturn(["code" => -1, "desc" => "图片上传失败"]);
            } else {
                $data['img'] = $ajaxImg['msg'];
            }
        }
        $id = I("post.id", "", "");
        if (empty($id)) {
            $cond_max['status'] = 1;
            $max_priority = M("jt_title_banner")->where($cond_max)->max("priority");
            $data['priority'] = $max_priority + 1;
            M("jt_title_banner")->add($data);
        } else {
            M("jt_title_banner")->where(['id' => $id])->save($data);
        }
        $this->ajaxReturn(["code" => 1, "desc" => "成功"]);
    }

    public function ajaxBannerStatus()
    {
        $id = I("post.id", "", "");
        $data['status'] = I("post.status", "", "");//---0:待发布，1发布,9删除----
        if (!empty($id)) {
            $id_arr = explode(",", $id);
            $cond['id'] = array("IN", $id_arr);
            M("jt_title_banner")->where($cond)->save($data);
        }
        $this->ajaxReturn(["code" => 1, "desc" => "成功"]);
    }

    public function ajaxBannerPriority()
    {
        $id = I("post.id", "", "");
        $data['priority'] = I("post.priority", "", "");
        if (!empty($id)) {
            $cond['id'] = $id;
            M("jt_title_banner")->where($cond)->save($data);
        }
        $this->ajaxReturn(["code" => 1, "desc" => "成功"]);
    }


    //---统计数据---
    private function pri_get_dept_data($type = 0)
    {
        $page = I("get.page", 1, "");
        $rank_type = I("get.rank_type", 0, ""); //----0:打卡率，1：打卡次数---
        $date_type = I("get.date_type", 0, "");//----0:今日，1：一周，2：月度，3：季度，4：年度---
        $start_date = $end_date = "";
        $end_date = date("Y-m-d H:i:s");
        switch ($date_type) {
            case 0:
                $date = date("Y-m-d");
                $start_date = $date . " 00:00:00";
                break;
            case 1:
                $week = date("w");
                $start_date = date("Y-m-d", strtotime('+' . 1 - $week . ' days')) . " 00:00:00";
                break;
            case 2:
                $start_date = date('Y-m-d', strtotime(date('Y-m', time()) . '-01 00:00:00'));
                break;
            case 3:
                $month = date('m');
                if ($month == 1 || $month == 2 || $month == 3) {
                    $start_date = strtotime(date('Y-01-01 00:00:00'));
                } elseif ($month == 4 || $month == 5 || $month == 6) {
                    $start_date = strtotime(date('Y-04-01 00:00:00'));
                } elseif ($month == 7 || $month == 8 || $month == 9) {
                    $start_date = strtotime(date('Y-07-01 00:00:00'));
                } else {
                    $start_date = strtotime(date('Y-10-01 00:00:00'));
                }
                break;
            case 4:
                $start_date = date('Y-01-01 00:00:00');
                break;
            default:
                break;
        }
        //-----获取在时间范围内的部门数据----
        $cond['add_time'] = array("between", array($start_date, $end_date));
        $cond['type'] = $type;
        $cond['status'] = 1;
        $dept_data_list_my = array();

        $count = M("jt_dept_data")->where($cond)->count();
        $number = $count / 100;
        for ($i = 0; $i <= $number; $i++) {
            $dept_data_list = M("jt_dept_data")->where($cond)->field("dept_id,number,clock")->limit($i * 100, 100)->select();
            //------部门统计数据整合--------
            foreach ($dept_data_list as $dept_data) {
                if (empty($dept_data_list_my[$dept_data['dept_id']])) {
                    $dept_data_list_my[$dept_data['dept_id']] = array("dept_id" => $dept_data['dept_id']);
                }
                if ($dept_data['clock'] > $dept_data['number']) {
                    $dept_data['clock'] = $dept_data['number'];
                }
                $dept_data_list_my[$dept_data['dept_id']]['number'] += $dept_data['number'];
                $dept_data_list_my[$dept_data['dept_id']]['clock'] += $dept_data['clock'];
            }
        }

        //-----计算部门完成率-----
        foreach ($dept_data_list_my as &$dept_data_my) {
            if ($dept_data_my['number'] == 0) {
                $dept_data_my['per'] = 0;
            } else {
                $dept_data_my['per'] = number_format($dept_data_my['clock'] * 100 / $dept_data_my['number'], 2);
            }
        }

        //-----按完成率倒叙排列----

        $flag = array();
        if ($rank_type == 0) {
            foreach ($dept_data_list_my as $dept_data_my1) {
                $flag[] = $dept_data_my1['per'];
            }
        } else {
            foreach ($dept_data_list_my as $dept_data_my1) {
                $flag[] = $dept_data_my1['clock'];
            }
        }

        array_multisort($flag, SORT_DESC, $dept_data_list_my);

        //-----截取部门排行榜数据----
        $count = count($dept_data_list_my);
        if ((($page * 10 - 10) < $count) && ($count < $page * 10)) {
            $dept_data_list_my = array_slice($dept_data_list_my, $page * 10 - 10, $count + 10 - $page * 10);
        } else if ($count >= $page * 10) {
            $dept_data_list_my = array_slice($dept_data_list_my, $page * 10 - 10, 10);
        } else {
            $dept_data_list_my = array();
        }

        //---根据截取的部门数据获取部门名称---
        $dept_id_arr = [];
        foreach ($dept_data_list_my as $dept_data_my_id) {
            $dept_id_arr[] = $dept_data_my_id['dept_id'];
        }
        if ($dept_id_arr) {
            $cond_dept_id_arr['id'] = array("IN", $dept_id_arr);
            $depts = M("jt_dept")->where($cond_dept_id_arr)->getField("id,name");
            foreach ($dept_data_list_my as &$dept_data_my) {
                $dept_data_my['dept_name'] = $depts[$dept_data_my['dept_id']];
            }
        }
        $res = ["count" => $count, "depts" => $dept_data_list_my];
        return $res;
    }

    private function pri_get_user_data()
    {
        $page = I("get.page", 1, "");
        $type = I("get.type", 0, ""); //---0:文章阅读排行,1:学习打卡排行---
        $date_type = I("get.date_type", "", "");//----0:今日，1：一周，2：月度，3：季度，4：年度---
        $start_date = $end_date = "";
        $end_date = date("Y-m-d H:i:s");
        switch ($date_type) {
            case 0:
                $date = date("Y-m-d");
                $start_date = $date . " 00:00:00";
                break;
            case 1:
                $week = date("w");
                $start_date = date("Y-m-d", strtotime('+' . 1 - $week . ' days')) . " 00:00:00";
                break;
            case 2:
                $start_date = date('Y-m-d', strtotime(date('Y-m', time()) . '-01 00:00:00'));
                break;
            case 3:
                $month = date('m');
                if ($month == 1 || $month == 2 || $month == 3) {
                    $start_date = strtotime(date('Y-01-01 00:00:00'));
                } elseif ($month == 4 || $month == 5 || $month == 6) {
                    $start_date = strtotime(date('Y-04-01 00:00:00'));
                } elseif ($month == 7 || $month == 8 || $month == 9) {
                    $start_date = strtotime(date('Y-07-01 00:00:00'));
                } else {
                    $start_date = strtotime(date('Y-10-01 00:00:00'));
                }
                break;
            case 4:
                $start_date = date('Y-01-01 00:00:00');
                break;
            default:
                break;
        }
        $cond['add_time'] = array("between", array($start_date, $end_date));
        $cond['type'] = $type;
        $cond['status'] = 1;
        $start = $page ? (($page - 1) * 10) : 0;
        $count = M("jt_user_clock")->where($cond)->field("count(distinct user_id) as count")->find();
        $count = $count['count'];
        $user_data_arr = M("jt_user_clock")->where($cond)->group("user_id")
            ->field("user_id,count(1) as count")->order("count desc, user_id asc")->limit($start, 10)->select();
        $user_id_arr = [];
        foreach ($user_data_arr as $user_data) {
            $user_id_arr[] = $user_data['user_id'];
        }
        if (!empty($user_id_arr)) {
            $cond_user_id['id'] = array("IN", $user_id_arr);
            $users = M("jt_user")->where($cond_user_id)->getField("id,name");//--获取用户姓名列表--


            $user_dept = M("jt_user")->where($cond_user_id)->getField("id, dept_id");
            $dept_id_arr = [];
            foreach ($user_dept as $dept_id){
                $dept_id_arr[] = $dept_id;
            }
            $depts = [];
            if(!empty($dept_id_arr)){
                $cond_dept_id['id'] = array("IN", $dept_id_arr);
                $depts = M("jt_dept")->where($cond_dept_id)->getField("id,name");//--获取用户部门列表---
            }

            foreach ($user_data_arr as &$user_data) {
                $user_data['user_name'] = $users[$user_data['user_id']];
                $user_data['dept_name'] = $depts[$user_dept[$user_data['user_id']]];
            }

        }
        $res = ["count" => $count, "users" => $user_data_arr];
        return $res;
    }

    public function data()
    {
//        $deptStudyChart = $this->pri_get_dept_data();
//        $deptViewChart = $this->pri_get_dept_data(1);
//        $userChart = $this->pri_get_user_data();
//        $res = ["deptStudyChart" => $deptStudyChart, "deptViewChart" => $deptViewChart, "userChart" => $userChart];
//        $this->assign("res", json_encode($res));
        $this->display();
    }

    public function ajaxDeptStudy()
    {
        $deptStudyChart = $this->pri_get_dept_data();
        $this->ajaxReturn(["res" => $deptStudyChart]);
    }

    public function ajaxDeptView()
    {
        $deptViewChart = $this->pri_get_dept_data(1);
        $this->ajaxReturn(["res" => $deptViewChart]);
    }

    public function ajaxUserClock()
    {
        $userChart = $this->pri_get_user_data();
        $this->ajaxReturn(["res" => $userChart]);
    }

    public function exportDeptStudy()
    {
        set_time_limit(0);
        vendor('phpexcel.PHPExcel');
        $objExcel = new \PHPExcel();
        $objActSheet = $objExcel->getActiveSheet();
        $is_per = I("get.is_per", 0, "");//----是否为打卡率排名0：不是，1：是---
        $objActSheet->getColumnDimension('A')->setWidth(20);
        $objActSheet->getColumnDimension('B')->setWidth(30);
        $objActSheet->getColumnDimension('C')->setWidth(20);
        $objActSheet->setCellValue('A1', "排名");
        $objActSheet->setCellValue('B1', "公司名称");
        if($is_per){
            $objActSheet->setCellValue('C1', "完成率");
        }else{
            $objActSheet->setCellValue('C1', "完成次数");
        }
        $line = 2;
        $type = 0;
        $date_type = I("get.date_type", "", "");//----0:今日，1：一周，2：月度，3：季度，4：年度---

        $start_date = $end_date = "";
        $end_date = date("Y-m-d H:i:s");
        switch ($date_type) {
            case 0:
                $date = date("Y-m-d");
                $start_date = $date . " 00:00:00";
                break;
            case 1:
                $week = date("w");
                $start_date = date("Y-m-d", strtotime('+' . 1 - $week . ' days')) . " 00:00:00";
                break;
            case 2:
                $start_date = date('Y-m-d', strtotime(date('Y-m', time()) . '-01 00:00:00'));
                break;
            case 3:
                $month = date('m');
                if ($month == 1 || $month == 2 || $month == 3) {
                    $start_date = strtotime(date('Y-01-01 00:00:00'));
                } elseif ($month == 4 || $month == 5 || $month == 6) {
                    $start_date = strtotime(date('Y-04-01 00:00:00'));
                } elseif ($month == 7 || $month == 8 || $month == 9) {
                    $start_date = strtotime(date('Y-07-01 00:00:00'));
                } else {
                    $start_date = strtotime(date('Y-10-01 00:00:00'));
                }
                break;
            case 4:
                $start_date = date('Y-01-01 00:00:00');
                break;
            default:
                break;
        }
        //-----获取在时间范围内的部门数据----
        $cond['add_time'] = array("between", array($start_date, $end_date));
        $cond['type'] = $type;
        $cond['status'] = 1;
        $dept_data_list_my = array();

        $count = M("jt_dept_data")->where($cond)->count();
        $number = $count / 100;
        for ($i = 0; $i <= $number; $i++) {
            $dept_data_list = M("jt_dept_data")->where($cond)->field("dept_id,number,clock")->limit($i * 100, 100)->select();
            //------部门统计数据整合--------
            foreach ($dept_data_list as $dept_data) {
                if (empty($dept_data_list_my[$dept_data['dept_id']])) {
                    $dept_data_list_my[$dept_data['dept_id']] = array("dept_id" => $dept_data['dept_id']);
                }
                if ($dept_data['clock'] > $dept_data['number']) {
                    $dept_data['clock'] = $dept_data['number'];
                }
                $dept_data_list_my[$dept_data['dept_id']]['number'] += $dept_data['number'];
                $dept_data_list_my[$dept_data['dept_id']]['clock'] += $dept_data['clock'];
            }
        }
        //-----计算部门完成率-----
        if ($is_per) {
            foreach ($dept_data_list_my as &$dept_data_my) {
                if ($dept_data_my['number'] == 0) {
                    $dept_data_my['per'] = 0;
                } else {
                    $dept_data_my['per'] = number_format($dept_data_my['clock'] * 100 / $dept_data_my['number'], 2);
                    if ($dept_data_my['per'] > 100.00) {
                        $dept_data_my['per'] = 100.00;
                    }
                }
            }
        }
        //-----按完成率倒叙排列----
        $flag = array();
        if ($is_per) {
            foreach ($dept_data_list_my as $dept_data_my1) {
                $flag[] = $dept_data_my1['per'];
            }
        } else {
            foreach ($dept_data_list_my as $dept_data_my1) {
                $flag[] = $dept_data_my1['clock'];
            }
        }

        array_multisort($flag, SORT_DESC, $dept_data_list_my);

        //---根据截取的部门数据获取部门名称---
        $dept_id_arr = [];
        foreach ($dept_data_list_my as $dept_data_my_id) {
            $dept_id_arr[] = $dept_data_my_id['dept_id'];
        }
        $cond_dept_id_arr['id'] = array("IN", $dept_id_arr);
        $depts = M("jt_dept")->where($cond_dept_id_arr)->getField("id,name");
        foreach ($dept_data_list_my as &$dept_data_my) {
            $dept_data_my['dept_name'] = $depts[$dept_data_my['dept_id']];
            $objActSheet->setCellValue('A' . $line, $line - 1);
            $objActSheet->setCellValue('B' . $line, $dept_data_my['dept_name']);
            if ($is_per) {
                $objActSheet->setCellValue('C' . $line, $dept_data_my['per'] . "%");
            } else {
                $objActSheet->setCellValue('C' . $line, $dept_data_my['clock']);
            }

            $line++;
        }

        $fileName = '公司学习排行' . date('Ymd') . '.xlsx';
        header('Content-Type: application/vnd.ms-excel');
        header("Content-Disposition: attachment;filename=\"$fileName\"");
        header('Cache-Control: max-age=0');
        $objWriter = new \PHPExcel_Writer_Excel2007($objExcel);
        $objWriter->save('php://output');
    }

    public function exportDeptView()
    {
        set_time_limit(0);
        vendor('phpexcel.PHPExcel');
        $objExcel = new \PHPExcel();
        $objActSheet = $objExcel->getActiveSheet();
        $is_per = I("get.is_per", 0, "");//----是否为打卡率排名0：不是，1：是---
        $objActSheet->getColumnDimension('A')->setWidth(20);
        $objActSheet->getColumnDimension('B')->setWidth(30);
        $objActSheet->getColumnDimension('C')->setWidth(20);
        $objActSheet->setCellValue('A1', "排名");
        $objActSheet->setCellValue('B1', "公司名称");
        if($is_per){
            $objActSheet->setCellValue('C1', "完成率");
        }else{
            $objActSheet->setCellValue('C1', "完成次数");
        }

        $line = 2;
        $type = 1;
        $date_type = I("get.date_type", "", "");//----0:今日，1：一周，2：月度，3：季度，4：年度---

        $start_date = $end_date = "";
        $end_date = date("Y-m-d H:i:s");
        switch ($date_type) {
            case 0:
                $date = date("Y-m-d");
                $start_date = $date . " 00:00:00";
                break;
            case 1:
                $week = date("w");
                $start_date = date("Y-m-d", strtotime('+' . 1 - $week . ' days')) . " 00:00:00";
                break;
            case 2:
                $start_date = date('Y-m-d', strtotime(date('Y-m', time()) . '-01 00:00:00'));
                break;
            case 3:
                $month = date('m');
                if ($month == 1 || $month == 2 || $month == 3) {
                    $start_date = strtotime(date('Y-01-01 00:00:00'));
                } elseif ($month == 4 || $month == 5 || $month == 6) {
                    $start_date = strtotime(date('Y-04-01 00:00:00'));
                } elseif ($month == 7 || $month == 8 || $month == 9) {
                    $start_date = strtotime(date('Y-07-01 00:00:00'));
                } else {
                    $start_date = strtotime(date('Y-10-01 00:00:00'));
                }
                break;
            case 4:
                $start_date = date('Y-01-01 00:00:00');
                break;
            default:
                break;
        }
        //-----获取在时间范围内的部门数据----
        $cond['add_time'] = array("between", array($start_date, $end_date));
        $cond['type'] = $type;
        $cond['status'] = 1;
        $dept_data_list_my = array();

        $count = M("jt_dept_data")->where($cond)->count();
        $number = $count / 100;
        for ($i = 0; $i <= $number; $i++) {
            $dept_data_list = M("jt_dept_data")->where($cond)->field("dept_id,number,clock")->limit($i * 100, 100)->select();
            //------部门统计数据整合--------
            foreach ($dept_data_list as $dept_data) {
                if (empty($dept_data_list_my[$dept_data['dept_id']])) {
                    $dept_data_list_my[$dept_data['dept_id']] = array("dept_id" => $dept_data['dept_id']);
                }
                if ($dept_data['clock'] > $dept_data['number']) {
                    $dept_data['clock'] = $dept_data['number'];
                }
                $dept_data_list_my[$dept_data['dept_id']]['number'] += $dept_data['number'];
                $dept_data_list_my[$dept_data['dept_id']]['clock'] += $dept_data['clock'];
            }
        }
        //-----计算部门完成率-----
        if ($is_per) {
            foreach ($dept_data_list_my as &$dept_data_my) {
                if ($dept_data_my['number'] == 0) {
                    $dept_data_my['per'] = 0;
                } else {
                    $dept_data_my['per'] = number_format($dept_data_my['clock'] * 100 / $dept_data_my['number'], 2);
                    if ($dept_data_my['per'] > 100.00) {
                        $dept_data_my['per'] = 100.00;
                    }
                }
            }
        }


        //-----按完成率倒叙排列----
        $flag = array();
        if ($is_per) {
            foreach ($dept_data_list_my as $dept_data_my1) {
                $flag[] = $dept_data_my1['per'];
            }
        } else {
            foreach ($dept_data_list_my as $dept_data_my1) {
                $flag[] = $dept_data_my1['clock'];
            }
        }

        array_multisort($flag, SORT_DESC, $dept_data_list_my);

        //---根据截取的部门数据获取部门名称---
        $dept_id_arr = [];
        foreach ($dept_data_list_my as $dept_data_my_id) {
            $dept_id_arr[] = $dept_data_my_id['dept_id'];
        }
        $cond_dept_id_arr['id'] = array("IN", $dept_id_arr);
        $depts = M("jt_dept")->where($cond_dept_id_arr)->getField("id,name");
        foreach ($dept_data_list_my as &$dept_data_my) {
            $dept_data_my['dept_name'] = $depts[$dept_data_my['dept_id']];
            $objActSheet->setCellValue('A' . $line, $line - 1);
            $objActSheet->setCellValue('B' . $line, $dept_data_my['dept_name']);
            if ($is_per) {
                $objActSheet->setCellValue('C' . $line, $dept_data_my['per'] . "%");
            } else {
                $objActSheet->setCellValue('C' . $line, $dept_data_my['clock']);
            }

            $line++;
        }

        $fileName = '公司打卡排行' . date('Ymd') . '.xlsx';
        header('Content-Type: application/vnd.ms-excel');
        header("Content-Disposition: attachment;filename=\"$fileName\"");
        header('Cache-Control: max-age=0');
        $objWriter = new \PHPExcel_Writer_Excel2007($objExcel);
        $objWriter->save('php://output');
    }

    public function exportUserClock()
    {
        set_time_limit(0);
        vendor('phpexcel.PHPExcel');
        $objExcel = new \PHPExcel();
        $objActSheet = $objExcel->getActiveSheet();
        $objActSheet->getColumnDimension('A')->setWidth(20);
        $objActSheet->getColumnDimension('B')->setWidth(30);
        $objActSheet->getColumnDimension('C')->setWidth(20);
        $objActSheet->setCellValue('A1', "用户名称");
        $objActSheet->setCellValue('B1', "公司名称");
        $objActSheet->setCellValue('C1', "打卡次数");
        $line = 2;
        $type = I("get.type", 0, "");
        $date_type = I("get.date_type", "", "");//----0:今日，1：一周，2：月度，3：季度，4：年度---
        $start_date = $end_date = "";
        $end_date = date("Y-m-d H:i:s");
        switch ($date_type) {
            case 0:
                $date = date("Y-m-d");
                $start_date = $date . " 00:00:00";
                break;
            case 1:
                $week = date("w");
                $start_date = date("Y-m-d", strtotime('+' . 1 - $week . ' days')) . " 00:00:00";
                break;
            case 2:
                $start_date = date('Y-m-d', strtotime(date('Y-m', time()) . '-01 00:00:00'));
                break;
            case 3:
                $month = date('m');
                if ($month == 1 || $month == 2 || $month == 3) {
                    $start_date = strtotime(date('Y-01-01 00:00:00'));
                } elseif ($month == 4 || $month == 5 || $month == 6) {
                    $start_date = strtotime(date('Y-04-01 00:00:00'));
                } elseif ($month == 7 || $month == 8 || $month == 9) {
                    $start_date = strtotime(date('Y-07-01 00:00:00'));
                } else {
                    $start_date = strtotime(date('Y-10-01 00:00:00'));
                }
                break;
            case 4:
                $start_date = date('Y-01-01 00:00:00');
                break;
            default:
                break;
        }
        $cond_dept['status'] = 1;
        $depts_s = M("jt_dept")->where($cond_dept)->select();
        foreach($depts_s as $key => $dept_s){
            if($key == 0){

            }else{
                $objExcel->createSheet();
                $objExcel->setActiveSheetIndex($key)->setTitle($dept_s['name']);
                $objExcel->setActiveSheetIndex($key);
                $objExcel->setActiveSheetIndex($key)->getColumnDimension('A')->setWidth(20);
                $objExcel->setActiveSheetIndex($key)->getColumnDimension('B')->setWidth(30);
                $objExcel->setActiveSheetIndex($key)->getColumnDimension('C')->setWidth(20);
                $objExcel->setActiveSheetIndex($key)->setCellValue('A1', "用户名称");
                $objExcel->setActiveSheetIndex($key)->setCellValue('B1', "公司名称");
                $objExcel->setActiveSheetIndex($key)->setCellValue('C1', "打卡次数");
                $line = 2;
            }
            $user_cond['dept_id'] = $dept_s['id'];
            $user_cond['status'] = 1;
            $count = M("jt_user")->where($user_cond)->count();
            $number = $count / 100;
            if(is_int($number) && ($number != 0)){
                $number = $number - 1;
            }
            for($i = 0; $i <= $number; $i++){
                $users = M("jt_user")->where($user_cond)->order("id asc")->limit($i * 100, 100)->getField("id, name");
                $user_id_arr = [];
                foreach ($users as $user_id => $user_data) {
                    $user_id_arr[] = $user_id;
                }
                $cond['type'] = $type;
                $cond['add_time'] = array("between", array($start_date, $end_date));
                $cond['status'] = 1;
                $cond['user_id'] = array("IN", $user_id_arr);
                $user_data_arr = M("jt_user_clock")->where($cond)->group("user_id")
                    ->getField("user_id,count(1) as count");

                foreach ($users as $user_id => $name) {
                    $user_data['user_name'] = $name;
                    $user_data['dept_name'] = $dept_s['name'];
                    $user_data['count'] = array_key_exists($user_id, $user_data_arr) ? $user_data_arr[$user_id] : 0;
                    if($key == 0){
                        $objActSheet->setCellValue('A' . $line, $name);
                        $objActSheet->setCellValue('B' . $line, $dept_s['name']);
                        $objActSheet->setCellValue('C' . $line, array_key_exists($user_id, $user_data_arr) ? $user_data_arr[$user_id] : 0);
                    }else{
                        $objExcel->setActiveSheetIndex($key)->setCellValue('A' . $line, $name);
                        $objExcel->setActiveSheetIndex($key)->setCellValue('B' . $line, $dept_s['name']);
                        $objExcel->setActiveSheetIndex($key)->setCellValue('C' . $line, array_key_exists($user_id, $user_data_arr) ? $user_data_arr[$user_id] : 0);
                    }

                    $line++;
                }
            }
            if($key == 0){
                $objActSheet->setTitle($dept_s['name']);
                $objExcel->setActiveSheetIndex($key);
            }else{

            }

        }
        /*$cond['type'] = $type;
        $cond['add_time'] = array("between", array($start_date, $end_date));
        $cond['status'] = 1;
        $count = M("jt_user_clock")->where($cond)->field("count(distinct user_id) as count")->find();
        $count = $count['count'];
        $number = $count / 100;
        for ($i = 0; $i <= $number; $i++) {
            $user_data_arr = M("jt_user_clock")->where($cond)->group("user_id")
                ->field("user_id,count(1) as count")->order("count desc,user_id desc")->limit($i * 100, 100)->select();
            $user_id_arr = [];
            foreach ($user_data_arr as $user_data) {
                $user_id_arr[] = $user_data['user_id'];
            }
            $cond_user_id['id'] = array("IN", $user_id_arr);
            $users = M("jt_user")->where($cond_user_id)->getField("id,name");//---获取员工姓名---

            //---获取部门列表---
            $dept_arr = M("jt_user")->where($cond_user_id)->getField("id,dept_id");
            $dept_id_arr = [];
            foreach ($dept_arr as $dept){
                $dept_id_arr[] = $dept;
            }
            $cond_dept_id['id'] = array("IN", $dept_id_arr);
            $depts = M("jt_dept")->where($cond_dept_id)->getField("id,name");//---获取公司名称--

            foreach ($user_data_arr as $user_data) {
                $user_data['user_name'] = $users[$user_data['user_id']];
                $user_data['dept_name'] = $depts[$dept_arr[$user_data['user_id']]];
                $objActSheet->setCellValue('A' . $line, $line - 1);
                $objActSheet->setCellValue('B' . $line, $user_data['user_name']);
                $objActSheet->setCellValue('C' . $line, $user_data['dept_name']);
                $objActSheet->setCellValue('D' . $line, $user_data['count']);
                $line++;
            }
        }*/


        $fileName = '个人打卡排行' . date('Ymd') . '.xlsx';
        header('Content-Type: application/vnd.ms-excel');
        header("Content-Disposition: attachment;filename=\"$fileName\"");
        header('Cache-Control: max-age=0');
        $objWriter = new \PHPExcel_Writer_Excel2007($objExcel);
        $objWriter->save('php://output');
    }

    //------公司管理------
    private function getDeptList()
    {
        $cond['status'] = array("NEQ", 9);
        $depts = M("jt_dept")->where($cond)->order("priority asc")->select();
        $dept_id = I("get.dept_id", "", "");
        $dept = $depts[0];
        if (empty($dept_id)) {
            $dept_id = $depts[0]['id'];
        } else {
            foreach ($depts as $v) {
                if ($v['id'] == $dept_id) {
                    $dept = $v;
                    break;
                }
            }
        }

        $user_cond['dept_id'] = $dept_id;
        $user_cond['status'] = array("NEQ", 9);
        $users = M("jt_user")->where($user_cond)->order("id asc")->limit(0, 10)->select();

        $res = ["depts" => $depts, "dept" => $dept, "users" => $users];
        return $res;
    }

    public function dept()
    {
        C('LAYOUT_ON', false);
//        $res = $this->getDeptList();
//        $this->assign("res", json_encode($res));
        $this->display();
    }

    public function ajaxDeptList()
    {
        $res = $this->getDeptList();
        $this->ajaxReturn(["res" => $res]);
    }

    public function ajaxDeptUpdate()
    {
        $id = I("post.id", "", "");
        if (!empty($id)) {//---修改內容--
            $priority = I("post.priority", "", "");
            $name = I("post.name", "", "");
            $status = I("post.status", "", "");
            if (!empty($priority) || !empty($name) || !empty($status)) {
                if (!empty($priority)) {
                    $data['priority'] = $priority;
                    M("jt_dept")->where(['id' => $id])->save($data);
                }
                if (!empty($name)) {
                    $data['name'] = $name;
                    M("jt_dept")->where(['id' => $id])->save($data);
                }
                if (!empty($status)) {
                    $res1 = $res2 = $res3 = $res4 = $res5 = 1;
                    M()->startTrans();
                    $data['status'] = $status;
                    $res1 = M("jt_dept")->where(['id' => $id])->save($data);
                    if ($data['status'] == 9) {
                        $cond_dept_delete['dept_id'] = $id;
                        M("jt_dept_data")->where($cond_dept_delete)->save($data);

                        $cond_user_delelte['dept_id'] = $id;
                        $cond_user_delelte['status'] = 1;
                        $users = M("jt_user")->where($cond_user_delelte)->getField("id");
                        if (!empty($users)) {
                            M("jt_user")->where($cond_user_delelte)->save($data);
                            $cond_user_delelte_id_arr['user_id'] = ["IN", $users];
                            M("jt_user_clock")->where($cond_user_delelte)->save($data);
                            M("jt_user_clock_total")->where($cond_user_delelte)->save($data);
                        }
                    }
                    if(!empty($res1) && !empty($res2) && !empty($res3) && !empty($res4) && !empty($res5)){
                        M()->commit();
                    }else{
                        M()->rollback();
                    }
                }
            }
        } else {
            $cond['name'] = $data['name'] = I("post.name", "", "");
            $data['priority'] = M("jt_title")->where($cond)->max("priority");
            $data['priority'] += 1;
            M("jt_dept")->add($data);
        }
        $this->ajaxReturn(["code" => 1]);
    }

    public function ajaxGetUserByDept()
    {
        $start_time = date("Y-m-d H:i:s");
        $dept_id = I("get.dept_id", "", "");
        $page = I("get.page", 1, "");
        $cond['dept_id'] = $dept_id;
        $cond['status'] = array("NEQ", 9);
        $users = M("jt_user")->where($cond)->order("id asc")->limit(($page - 1) * 10, 10)->select();


        //---获取部门信息---
        $dept = M("jt_dept")->where(["id" => $dept_id])->find();
        $count = $dept['reg_number'];
        $end_time = date("Y-m-d H:i:s");
        $res = ["dept" => $dept, "users" => $users, "count" => $count, "start_time" => $start_time, "end_time" => $end_time];
        $this->ajaxReturn(["res" => $res]);
    }

    public function ajaxGetUserByName()
    {
        $name = I("get.name", "", "");
        $cond['name'] = $name;
        $cond['status'] = 1;
        $users = M("jt_user")->where($cond)->select();
        $dept_id_arr = [];
        foreach ($users as $user){
            $dept_id_arr[] = $user['dept_id'];
        }
        if(!empty($dept_id_arr)){
            $cond_dept['dept_id'] = array("IN", $dept_id_arr);
            $depts = M("jt_dept")->where($cond_dept)->getField("id, name");
            foreach ($users as &$user1){
                if(array_key_exists($user1['dept_id'], $depts)){
                    $user1['dept_name'] = $depts[$user1['dept_id']];
                }
            }
        }
        $this->ajaxReturn(["res" => $users]);
    }

    public function ajaxUserUpdate()
    {
        $id = I("post.id", "", "");
        if (!empty($id)) {//---修改內容--
            $dept_id = I("post.dept_id", "", "");
            $name = I("post.name", "", "");
            $status = I("post.status", "", "");
            if (!empty($dept_id) || !empty($name) || !empty($status)) {
                if (!empty($dept_id)) {

                    $user = M("jt_user")->where(['id' => $id])->find();
                    $data['dept_id'] = $dept_id;
                    M("jt_user")->where(['id' => $id])->save($data);
                }
                if (!empty($name)) {
                    $data['name'] = $name;
                    M("jt_user")->where(['id' => $id])->save($data);
                }
                if (!empty($status)) {
                    $res1 = $res2 = $res3 = $res4 = 1;
                    M()->startTrans();
                    $data['status'] = $status;
                    $res1 = M("jt_user")->where(['id' => $id])->save($data);

                    $user = M("jt_user")->where(['id' => $id])->find();
                    S("jt_user3" . $user['openid'], null);
                    if ($status == 9) {
                        $cond_delete['user_id'] = $id;
                        $data_delete_all['status'] = 9;
                        M("jt_user_clock_total")->where($cond_delete)->save($data_delete_all);

                        $data_delete['status'] = 9;
                        M("jt_user_clock")->where($cond_delete)->save($data_delete);

                        $cond_delete_dept_number['id'] = $user['dept_id'];
                        $res4 = M("jt_dept")->where($cond_delete_dept_number)->setDec("reg_number");
                    }
                    M()->commit();
                }
            }
        }
        $this->ajaxReturn(["code" => 1]);
    }

    public function exportDept()
    {
        set_time_limit(0);
        vendor('phpexcel.PHPExcel');
        $objExcel = new \PHPExcel();
        $objActSheet = $objExcel->getActiveSheet();
        $objActSheet->getColumnDimension('A')->setWidth(20);
        $objActSheet->getColumnDimension('B')->setWidth(30);
        $objActSheet->getColumnDimension('C')->setWidth(20);
        $objActSheet->setCellValue('A1', "编号");
        $objActSheet->setCellValue('B1', "公司名称");
        $objActSheet->setCellValue('C1', "总人数");
        $line = 2;
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, "http://mobile.cncico.com:9483/apis/getToken");
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_TIMEOUT, 500);
        curl_setopt($ch, CURLOPT_COOKIEFILE, $this->lastCookieFile);
        $post_data = array("loginName" => "jtgzh", "password" => "21xIUwXJuHzaOSEb4i8q7w==");
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
        $out = curl_exec($ch);
        curl_close($ch);
        $content = json_decode($out);
        foreach ($content as $key => $val) {
            if ($key === "result") {//----result记录token信息---
                $ch1 = curl_init();
                curl_setopt($ch1, CURLOPT_URL, "http://mobile.cncico.com:9483/apis/api/zb/company/");
                curl_setopt($ch1, CURLOPT_HTTPGET, true);
                curl_setopt($ch1, CURLOPT_RETURNTRANSFER, 1);
                $headers = [
                    "Authorization:" . $val
                ];
                curl_setopt($ch1, CURLOPT_HTTPHEADER, $headers);
                $out1 = curl_exec($ch1);
                curl_close($ch1);
                $content1 = json_decode($out1);
                foreach ($content1 as $key1 => $val1) {
                    if ($key1 === "result") {
                        foreach ($val1 as $data) {
                            $data = get_object_vars($data);
                            $objActSheet->setCellValue('A' . $line, $data['orgCode']);
                            $objActSheet->setCellValue('B' . $line, $data['company']);
                            $objActSheet->setCellValue('C' . $line, $data['amount']);
                            $line++;
                        }
                    }
                }
            }
        }
        $fileName = '公司人数下载' . date('Ymd') . '.xlsx';
        header('Content-Type: application/vnd.ms-excel');
        header("Content-Disposition: attachment;filename=\"$fileName\"");
        header('Cache-Control: max-age=0');
        $objWriter = new \PHPExcel_Writer_Excel2007($objExcel);
        $objWriter->save('php://output');
    }

    public function importDept()
    {
        set_time_limit(0);
        $upload = new \Think\Upload();// 实例化上传类--
        $upload->maxSize = 200 * 1024 * 1024;// 设置附件上传大小--
        $upload->exts = array("xlsx", "xls");// 设置附件上传类型--
        $upload->rootPath = 'Public/Uploads/jt/'; // 设置附件上传根目录---
        $upload->savePath = ''; // 设置附件上传（子）目录--
        // 上传文件--
        $info = $upload->upload();
        if (!$info) {// 上传错误提示错误信息-----
            $this->error($upload->getError());
        } else {// 上传成功--
            vendor('phpexcel.PHPExcel');
            $objExcel = new \PHPExcel();
            $file = 'Public/Uploads/jt/' . date("Y-m-d") . "/" . $info['file']['savename'];
            $type = pathinfo($file);
            $type = strtolower($type["extension"]);
            $type = $type === 'csv' ? $type : 'Excel2007';     //这里需要与格式对应----

            // 判断使用哪种格式--
            $objReader = \PHPExcel_IOFactory::createReader($type);
            $objPHPExcel = $objReader->load($file);
            $sheet = $objPHPExcel->getSheet(0);
            $allColumn = $sheet->getHighestColumn();
            $allRow = $sheet->getHighestRow();        //**取得一共有多少行*/

            $data = array();
            for ($rowIndex = 2; $rowIndex <= $allRow; $rowIndex++) {        //循环读取每个单元格的内容。注意行从1开始，列从A开始----
                $data_unit = array();
                for ($colIndex = 'A'; $colIndex <= $allColumn; $colIndex++) {
                    $addr = $colIndex . $rowIndex;
                    $cell = $sheet->getCell($addr)->getValue();
                    if ($cell instanceof \PHPExcel_RichText) { //富文本转换字符串--
                        $cell = $cell->__toString();
                    }
                    switch ($colIndex) {
                        case "A":
                            $data_unit['code'] = $cell;
                            break;
                        case "B":
                            $data_unit['name'] = $cell;
                            break;
                        case "C":
                            $data_unit['number'] = $cell;
                            break;
                        default:
                            break;
                    }
                }
                $cond['code'] = $data_unit['code'];
                $cond['status'] = 1;
                $dept = M("jt_dept")->where($cond)->find();
                if (!empty($dept)) {
                    M("jt_dept")->where($cond)->save($data_unit);
                } else {
                    $cond_max['status'] = 1;
                    $max_priority = M("jt_dept")->where($cond_max)->max("priority");
                    $data_unit['priority'] = $max_priority + 1;
                    M("jt_dept")->where($cond)->add($data_unit);
                }
            }
            unlink($file);
            @rmdir('Public/Uploads/jt/' . date("Y-m-d") . "/");
        }
        $this->ajaxReturn(["code" => 1]);
    }

    public function exportDeptUsers()
    {
        set_time_limit(0);

        $name = date("Y-m-d");
        vendor('phpexcel.PHPExcel');
        $objExcel = new \PHPExcel();
        $objActSheet = $objExcel->getActiveSheet();
        $objActSheet->getColumnDimension('A')->setWidth(20);
        $objActSheet->setCellValue('A1', "已注册员工名称");
        $line = 2;
        $dept_id = I("get.dept_id", "", "");
        if(!empty($dept_id)){
            $dept = M("jt_dept")->where(['id' => $dept_id])->find();
            if(!empty($dept)){
                $name = $dept['name'] . $name;
                $objActSheet->setCellValue('A1', "已注册员工名称" . "（应注册". $dept['number'] . "人，实际注册" . $dept['reg_number'] . "人）");
            }
            $cond['dept_id'] = $dept_id;
            $cond['status'] = 1;
            $count = M("jt_user")->where($cond)->count();
            $number = $count / 100;
            for ($i = 0; $i <= $number; $i++) {
                $user_data_arr = M("jt_user")->where($cond)->limit($i * 100, 100)->select();
                foreach ($user_data_arr as $user_data) {
                    $objActSheet->setCellValue('A' . $line, $user_data['name']);
                    $line++;
                }
            }
        }
        $fileName = $name . '.xlsx';
        header('Content-Type: application/vnd.ms-excel');
        header("Content-Disposition: attachment;filename=\"$fileName\"");
        header('Cache-Control: max-age=0');
        $objWriter = new \PHPExcel_Writer_Excel2007($objExcel);
        $objWriter->save('php://output');
    }

    public function getUserDetail()
    {
        $user_id = I("get.user_id", "", "");
        if(!empty($user_id)){
            $cond['user_id'] = $user_id;
            $news_cond['clock'] = 1;
            $news_cond['status'] = array("NEQ", 9);
            $news = M("jt_news")->where($news_cond)->order("id desc")->limit(50)->select();
            $new_id_arr = [];
            foreach($news as $new){
                $new_id_arr[] = $new['id'];
            }
            if(!empty($new_id_arr)){
                $cond['new_id'] = array("IN", $new_id_arr);
            }
            $user_news = M("jt_user_clock")->where($cond)->getField("new_id,add_time");
            $clocked = $unclock = [];
            foreach ($news as $new){
                $data['id'] = $new['id'];
                $data['name'] = $new['title'];
                if(array_key_exists($new['id'], $user_news)){
                    $data['time'] = $user_news[$new['id']];
                    $clocked[] = $data;
                }else{
                    $unclock[] = $data;
                }
            }
            echo "已阅读：</br>";
            foreach ($clocked as $clock){
                echo $clock['id'] . ":" . $clock['name'] . " " . "阅读时间：" . $clock['time'] . "<br>";
            }

            echo "未阅读：</br>";
            foreach ($unclock as $clock){
                echo $clock['id'] . ":" . $clock['name'] . "<br>";
            }
        }
    }
}
